/*
* Copyright 2004-2005 mbsx@seznam.cz
* All Rights Reserved.
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace DeskNotes
{
	/// <summary>
	/// Display archived desknotes in hierarchy organized by categories.
	/// </summary>
	public class ArchiveForm : System.Windows.Forms.Form
	{
		internal static int EXPANDED_IMAGE = 1;
		internal static int COLLAPSED_IMAGE = 0;
		internal static int NOTE_IMAGE = 2;
		
		private bool currentNoteChanged = false;
		private TreeNode selectedNode = null;
		internal DeskNotes deskNotes;

		private Color backColor, textColor;

        private System.Windows.Forms.Splitter splitter1;
        internal System.Windows.Forms.ImageList imgListTree;
        private System.Windows.Forms.TreeView treeArchive;
        private System.Windows.Forms.ToolTip toolTip;
        private ToolStripContainer toolStripContainer1;
        private Panel panel1;
        private RichTextBox txtNote;
        private Label label1;
        private ToolStrip toolStripRTF;
        private ToolStripButton btnBullet;
        private ToolStripSeparator toolStripSeparator4;
        private ToolStripButton btnBold;
        private ToolStripButton btnItalic;
        private ToolStripButton btnStrike;
        private ToolStripButton btnUnderline;
        private ToolStripSeparator toolStripSeparator5;
        private ToolStripButton btnIndentRight;
        private ToolStripSeparator toolStripSeparator6;
        private ToolStripButton btnFont;
        private ToolStripButton btnBackColor;
        private StatusStrip statusStrip1;
        private ToolStripStatusLabel lblArchivedTime;
        private ToolStripStatusLabel lblMod;
        private ToolStripButton btnSave;
        private ToolStripSeparator toolStripSeparator2;
        private ToolStripSeparator toolStripSeparator1;
        private ToolStripStatusLabel toolStripStatusLabel1;
        private ToolStripStatusLabel lblModificationTime;
        private FontDialog fontDialog;
        private ColorDialog colorDialog;
        private ContextMenuStrip ctxMenuArchNote;
        private ToolStripMenuItem toolbarStripMenuItem;
        private ToolStripMenuItem statusbarStripMenuItem;
        private ToolStripSeparator toolStripSeparator3;
        private ToolStripMenuItem printStripMenuItem;
        private ContextMenuStrip ctxMenuCategory;
        private ToolStripMenuItem newCategoryStripMenuItem;
        private ToolStripMenuItem removeStripMenuItem;
        private ToolStripSeparator toolStripSeparator7;
        private ToolStripMenuItem printPreviewStripMenuItem;
        private ToolStripMenuItem printCategoryStripMenuItem;
        private ContextMenuStrip ctxMenuTreeNote;
        private ToolStripMenuItem moveToDesktopStripMenuItem;
        private ToolStripMenuItem copyToDesktopStripMenuItem;
        private ToolStripSeparator toolStripSeparator8;
        private ToolStripMenuItem removeNoteStripMenuItem;
        private ToolStripSeparator toolStripSeparator9;
        private ToolStripMenuItem printToolStripMenuItem;
		private System.ComponentModel.IContainer components;
				

		/// <summary>
		/// DeskNotes property Notes can be set only once for ArchiveForm
		/// It is done to prevent desknotes and archive to be accidentally changed
		/// by different forms
		/// </summary>
		internal DeskNotes Notes
		{
			set 
			{
				if (null == deskNotes)
					deskNotes = value;
			}
		}

		/// <summary>
		/// Changes forms UI to indicate note or category was changed.
		/// Called when note is archived, modified or removed. 
		/// Also when category is added, renamed and removed.
		/// </summary>
		private void NeedSave()
		{
			btnSave.Enabled = true;
		}

		/// <summary>
		/// When archive was saved then reset UI to indicate that.
		/// </summary>
		private void ArchiveSaved()
		{
			foreach(TreeNode node in treeArchive.Nodes)
				((DeskNotesTreeNode)node).ResetEdited();

			btnSave.Enabled = false;
		}


		public ArchiveForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			
		}

		/// <summary>
		/// Creates archive when called from Note's menu or from sys tray icon menu.
		/// </summary>
		/// <param name="notes">DeskNotes handling archive dataset.</param>
		internal ArchiveForm(DeskNotes notes)
		{
			InitializeComponent();
            Properties.Settings settings = Properties.Settings.Default;
            if (0 != settings.ArchivePosition.Width)
            {
                Rectangle position = settings.ArchivePosition;
                this.StartPosition = FormStartPosition.Manual;
                this.Left = position.X;
                this.Top = position.Y;
                this.Width = position.Width;
                this.Height = position.Height;
            }
            if (-1 != settings.ArchiveSplitter)
            {
                splitter1.SplitPosition = settings.ArchiveSplitter;
            }
			this.Notes = notes;
			notes.archiveForm = this;
			this.Disposed += new EventHandler(notes.archiveForm_Disposed);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ArchiveForm));
            System.Windows.Forms.ToolStripMenuItem insertTimeStripMenuItem;
            System.Windows.Forms.ToolStripButton btnIndentLeft;
            this.toolStripContainer1 = new System.Windows.Forms.ToolStripContainer();
            this.statusStrip1 = new System.Windows.Forms.StatusStrip();
            this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
            this.lblArchivedTime = new System.Windows.Forms.ToolStripStatusLabel();
            this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
            this.lblMod = new System.Windows.Forms.ToolStripStatusLabel();
            this.lblModificationTime = new System.Windows.Forms.ToolStripStatusLabel();
            this.panel1 = new System.Windows.Forms.Panel();
            this.txtNote = new System.Windows.Forms.RichTextBox();
            this.ctxMenuArchNote = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.toolbarStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.statusbarStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
            this.printStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.label1 = new System.Windows.Forms.Label();
            this.toolStripRTF = new System.Windows.Forms.ToolStrip();
            this.btnSave = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.btnBullet = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator4 = new System.Windows.Forms.ToolStripSeparator();
            this.btnBold = new System.Windows.Forms.ToolStripButton();
            this.btnItalic = new System.Windows.Forms.ToolStripButton();
            this.btnStrike = new System.Windows.Forms.ToolStripButton();
            this.btnUnderline = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator5 = new System.Windows.Forms.ToolStripSeparator();
            this.btnIndentRight = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator6 = new System.Windows.Forms.ToolStripSeparator();
            this.btnFont = new System.Windows.Forms.ToolStripButton();
            this.btnBackColor = new System.Windows.Forms.ToolStripButton();
            this.treeArchive = new System.Windows.Forms.TreeView();
            this.imgListTree = new System.Windows.Forms.ImageList(this.components);
            this.splitter1 = new System.Windows.Forms.Splitter();
            this.toolTip = new System.Windows.Forms.ToolTip(this.components);
            this.ctxMenuCategory = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.newCategoryStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.removeStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator7 = new System.Windows.Forms.ToolStripSeparator();
            this.printPreviewStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.printCategoryStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.ctxMenuTreeNote = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.moveToDesktopStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.copyToDesktopStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator8 = new System.Windows.Forms.ToolStripSeparator();
            this.removeNoteStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator9 = new System.Windows.Forms.ToolStripSeparator();
            this.printToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.fontDialog = new System.Windows.Forms.FontDialog();
            this.colorDialog = new System.Windows.Forms.ColorDialog();
            insertTimeStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            btnIndentLeft = new System.Windows.Forms.ToolStripButton();
            this.toolStripContainer1.BottomToolStripPanel.SuspendLayout();
            this.toolStripContainer1.ContentPanel.SuspendLayout();
            this.toolStripContainer1.TopToolStripPanel.SuspendLayout();
            this.toolStripContainer1.SuspendLayout();
            this.statusStrip1.SuspendLayout();
            this.panel1.SuspendLayout();
            this.ctxMenuArchNote.SuspendLayout();
            this.toolStripRTF.SuspendLayout();
            this.ctxMenuCategory.SuspendLayout();
            this.ctxMenuTreeNote.SuspendLayout();
            this.SuspendLayout();
            // 
            // toolStripContainer1
            // 
            // 
            // toolStripContainer1.BottomToolStripPanel
            // 
            this.toolStripContainer1.BottomToolStripPanel.Controls.Add(this.statusStrip1);
            // 
            // toolStripContainer1.ContentPanel
            // 
            this.toolStripContainer1.ContentPanel.Controls.Add(this.panel1);
            resources.ApplyResources(this.toolStripContainer1.ContentPanel, "toolStripContainer1.ContentPanel");
            resources.ApplyResources(this.toolStripContainer1, "toolStripContainer1");
            this.toolStripContainer1.Name = "toolStripContainer1";
            // 
            // toolStripContainer1.TopToolStripPanel
            // 
            this.toolStripContainer1.TopToolStripPanel.Controls.Add(this.toolStripRTF);
            // 
            // statusStrip1
            // 
            resources.ApplyResources(this.statusStrip1, "statusStrip1");
            this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripStatusLabel1,
            this.lblArchivedTime,
            this.toolStripSeparator2,
            this.lblMod,
            this.lblModificationTime});
            this.statusStrip1.Name = "statusStrip1";
            // 
            // toolStripStatusLabel1
            // 
            this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
            resources.ApplyResources(this.toolStripStatusLabel1, "toolStripStatusLabel1");
            // 
            // lblArchivedTime
            // 
            this.lblArchivedTime.BorderStyle = System.Windows.Forms.Border3DStyle.Etched;
            this.lblArchivedTime.Name = "lblArchivedTime";
            resources.ApplyResources(this.lblArchivedTime, "lblArchivedTime");
            // 
            // toolStripSeparator2
            // 
            this.toolStripSeparator2.Name = "toolStripSeparator2";
            resources.ApplyResources(this.toolStripSeparator2, "toolStripSeparator2");
            // 
            // lblMod
            // 
            this.lblMod.Name = "lblMod";
            resources.ApplyResources(this.lblMod, "lblMod");
            // 
            // lblModificationTime
            // 
            this.lblModificationTime.BorderStyle = System.Windows.Forms.Border3DStyle.Etched;
            this.lblModificationTime.Name = "lblModificationTime";
            resources.ApplyResources(this.lblModificationTime, "lblModificationTime");
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.txtNote);
            this.panel1.Controls.Add(this.label1);
            resources.ApplyResources(this.panel1, "panel1");
            this.panel1.Name = "panel1";
            // 
            // txtNote
            // 
            this.txtNote.AcceptsTab = true;
            this.txtNote.AutoWordSelection = true;
            this.txtNote.BackColor = System.Drawing.Color.White;
            resources.ApplyResources(this.txtNote, "txtNote");
            this.txtNote.ContextMenuStrip = this.ctxMenuArchNote;
            this.txtNote.Name = "txtNote";
            this.toolTip.SetToolTip(this.txtNote, resources.GetString("txtNote.ToolTip"));
            this.txtNote.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtNote_KeyDown);
            this.txtNote.SelectionChanged += new System.EventHandler(this.txtNote_SelectionChanged);
            // 
            // ctxMenuArchNote
            // 
            this.ctxMenuArchNote.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolbarStripMenuItem,
            this.statusbarStripMenuItem,
            this.toolStripSeparator3,
            insertTimeStripMenuItem,
            this.printStripMenuItem});
            this.ctxMenuArchNote.Name = "ctxMenuArchNote";
            resources.ApplyResources(this.ctxMenuArchNote, "ctxMenuArchNote");
            this.ctxMenuArchNote.Opening += new System.ComponentModel.CancelEventHandler(this.ctxMenuArchNote_Opening);
            // 
            // toolbarStripMenuItem
            // 
            this.toolbarStripMenuItem.CheckOnClick = true;
            this.toolbarStripMenuItem.Name = "toolbarStripMenuItem";
            resources.ApplyResources(this.toolbarStripMenuItem, "toolbarStripMenuItem");
            this.toolbarStripMenuItem.Click += new System.EventHandler(this.toolbarStripMenuItem_Click);
            // 
            // statusbarStripMenuItem
            // 
            this.statusbarStripMenuItem.Name = "statusbarStripMenuItem";
            resources.ApplyResources(this.statusbarStripMenuItem, "statusbarStripMenuItem");
            this.statusbarStripMenuItem.Click += new System.EventHandler(this.statusbarStripMenuItem_Click);
            // 
            // toolStripSeparator3
            // 
            this.toolStripSeparator3.Name = "toolStripSeparator3";
            resources.ApplyResources(this.toolStripSeparator3, "toolStripSeparator3");
            // 
            // insertTimeStripMenuItem
            // 
            insertTimeStripMenuItem.Name = "insertTimeStripMenuItem";
            resources.ApplyResources(insertTimeStripMenuItem, "insertTimeStripMenuItem");
            insertTimeStripMenuItem.Click += new System.EventHandler(this.insertTimeStripMenuItem_Click);
            // 
            // printStripMenuItem
            // 
            resources.ApplyResources(this.printStripMenuItem, "printStripMenuItem");
            this.printStripMenuItem.Name = "printStripMenuItem";
            this.printStripMenuItem.Click += new System.EventHandler(this.printStripMenuItem_Click);
            // 
            // label1
            // 
            resources.ApplyResources(this.label1, "label1");
            this.label1.Name = "label1";
            // 
            // toolStripRTF
            // 
            this.toolStripRTF.AllowItemReorder = true;
            resources.ApplyResources(this.toolStripRTF, "toolStripRTF");
            this.toolStripRTF.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.btnSave,
            this.toolStripSeparator1,
            this.btnBullet,
            this.toolStripSeparator4,
            this.btnBold,
            this.btnItalic,
            this.btnStrike,
            this.btnUnderline,
            this.toolStripSeparator5,
            this.btnIndentRight,
            btnIndentLeft,
            this.toolStripSeparator6,
            this.btnFont,
            this.btnBackColor});
            this.toolStripRTF.Name = "toolStripRTF";
            this.toolStripRTF.Stretch = true;
            // 
            // btnSave
            // 
            this.btnSave.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            resources.ApplyResources(this.btnSave, "btnSave");
            this.btnSave.Name = "btnSave";
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // toolStripSeparator1
            // 
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            resources.ApplyResources(this.toolStripSeparator1, "toolStripSeparator1");
            // 
            // btnBullet
            // 
            this.btnBullet.CheckOnClick = true;
            this.btnBullet.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            resources.ApplyResources(this.btnBullet, "btnBullet");
            this.btnBullet.Name = "btnBullet";
            this.btnBullet.Click += new System.EventHandler(this.btnBullet_Click);
            // 
            // toolStripSeparator4
            // 
            this.toolStripSeparator4.Name = "toolStripSeparator4";
            resources.ApplyResources(this.toolStripSeparator4, "toolStripSeparator4");
            // 
            // btnBold
            // 
            this.btnBold.CheckOnClick = true;
            this.btnBold.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            resources.ApplyResources(this.btnBold, "btnBold");
            this.btnBold.Name = "btnBold";
            this.btnBold.Click += new System.EventHandler(this.btnBold_Click);
            // 
            // btnItalic
            // 
            this.btnItalic.CheckOnClick = true;
            this.btnItalic.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            resources.ApplyResources(this.btnItalic, "btnItalic");
            this.btnItalic.Name = "btnItalic";
            this.btnItalic.Click += new System.EventHandler(this.btnItalic_Click);
            // 
            // btnStrike
            // 
            this.btnStrike.CheckOnClick = true;
            this.btnStrike.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            resources.ApplyResources(this.btnStrike, "btnStrike");
            this.btnStrike.Name = "btnStrike";
            this.btnStrike.Click += new System.EventHandler(this.btnStrike_Click);
            // 
            // btnUnderline
            // 
            this.btnUnderline.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            resources.ApplyResources(this.btnUnderline, "btnUnderline");
            this.btnUnderline.Name = "btnUnderline";
            this.btnUnderline.Click += new System.EventHandler(this.btnUnderline_Click);
            // 
            // toolStripSeparator5
            // 
            this.toolStripSeparator5.Name = "toolStripSeparator5";
            resources.ApplyResources(this.toolStripSeparator5, "toolStripSeparator5");
            // 
            // btnIndentRight
            // 
            this.btnIndentRight.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            resources.ApplyResources(this.btnIndentRight, "btnIndentRight");
            this.btnIndentRight.Name = "btnIndentRight";
            this.btnIndentRight.Click += new System.EventHandler(this.btnIndentRight_Click);
            // 
            // btnIndentLeft
            // 
            btnIndentLeft.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            resources.ApplyResources(btnIndentLeft, "btnIndentLeft");
            btnIndentLeft.Name = "btnIndentLeft";
            btnIndentLeft.Click += new System.EventHandler(this.btnIndentLeft_Click);
            // 
            // toolStripSeparator6
            // 
            this.toolStripSeparator6.Name = "toolStripSeparator6";
            resources.ApplyResources(this.toolStripSeparator6, "toolStripSeparator6");
            // 
            // btnFont
            // 
            this.btnFont.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            resources.ApplyResources(this.btnFont, "btnFont");
            this.btnFont.Name = "btnFont";
            this.btnFont.Click += new System.EventHandler(this.btnFont_Click);
            // 
            // btnBackColor
            // 
            this.btnBackColor.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            resources.ApplyResources(this.btnBackColor, "btnBackColor");
            this.btnBackColor.Name = "btnBackColor";
            this.btnBackColor.Click += new System.EventHandler(this.btnBackColor_Click);
            // 
            // treeArchive
            // 
            this.treeArchive.AllowDrop = true;
            resources.ApplyResources(this.treeArchive, "treeArchive");
            this.treeArchive.HotTracking = true;
            this.treeArchive.ImageList = this.imgListTree;
            this.treeArchive.ItemHeight = 16;
            this.treeArchive.LabelEdit = true;
            this.treeArchive.Name = "treeArchive";
            this.toolTip.SetToolTip(this.treeArchive, resources.GetString("treeArchive.ToolTip"));
            this.treeArchive.AfterCollapse += new System.Windows.Forms.TreeViewEventHandler(this.treeArchive_AfterCollapse);
            this.treeArchive.AfterLabelEdit += new System.Windows.Forms.NodeLabelEditEventHandler(this.treeArchive_AfterLabelEdit);
            this.treeArchive.MouseUp += new System.Windows.Forms.MouseEventHandler(this.treeArchive_MouseUp);
            this.treeArchive.DragDrop += new System.Windows.Forms.DragEventHandler(this.treeArchive_DragDrop);
            this.treeArchive.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeArchive_AfterSelect);
            this.treeArchive.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeArchive_MouseDown);
            this.treeArchive.DragEnter += new System.Windows.Forms.DragEventHandler(this.treeArchive_DragEnter);
            this.treeArchive.BeforeLabelEdit += new System.Windows.Forms.NodeLabelEditEventHandler(this.treeArchive_BeforeLabelEdit);
            this.treeArchive.BeforeSelect += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeArchive_BeforeSelect);
            this.treeArchive.AfterExpand += new System.Windows.Forms.TreeViewEventHandler(this.treeArchive_AfterExpand);
            this.treeArchive.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeArchive_ItemDrag);
            this.treeArchive.DragOver += new System.Windows.Forms.DragEventHandler(this.treeArchive_DragOver);
            // 
            // imgListTree
            // 
            this.imgListTree.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imgListTree.ImageStream")));
            this.imgListTree.TransparentColor = System.Drawing.Color.Transparent;
            this.imgListTree.Images.SetKeyName(0, "");
            this.imgListTree.Images.SetKeyName(1, "");
            this.imgListTree.Images.SetKeyName(2, "");
            // 
            // splitter1
            // 
            resources.ApplyResources(this.splitter1, "splitter1");
            this.splitter1.Name = "splitter1";
            this.splitter1.TabStop = false;
            // 
            // ctxMenuCategory
            // 
            this.ctxMenuCategory.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.newCategoryStripMenuItem,
            this.removeStripMenuItem,
            this.toolStripSeparator7,
            this.printPreviewStripMenuItem,
            this.printCategoryStripMenuItem});
            this.ctxMenuCategory.Name = "ctxMenuCategory";
            resources.ApplyResources(this.ctxMenuCategory, "ctxMenuCategory");
            // 
            // newCategoryStripMenuItem
            // 
            this.newCategoryStripMenuItem.Name = "newCategoryStripMenuItem";
            resources.ApplyResources(this.newCategoryStripMenuItem, "newCategoryStripMenuItem");
            this.newCategoryStripMenuItem.Click += new System.EventHandler(this.menuNewCategory_Click);
            // 
            // removeStripMenuItem
            // 
            resources.ApplyResources(this.removeStripMenuItem, "removeStripMenuItem");
            this.removeStripMenuItem.Name = "removeStripMenuItem";
            this.removeStripMenuItem.Click += new System.EventHandler(this.menuRemoveCategory_Click);
            // 
            // toolStripSeparator7
            // 
            this.toolStripSeparator7.Name = "toolStripSeparator7";
            resources.ApplyResources(this.toolStripSeparator7, "toolStripSeparator7");
            // 
            // printPreviewStripMenuItem
            // 
            this.printPreviewStripMenuItem.Name = "printPreviewStripMenuItem";
            resources.ApplyResources(this.printPreviewStripMenuItem, "printPreviewStripMenuItem");
            this.printPreviewStripMenuItem.Click += new System.EventHandler(this.menuPrintPreview_Click);
            // 
            // printCategoryStripMenuItem
            // 
            resources.ApplyResources(this.printCategoryStripMenuItem, "printCategoryStripMenuItem");
            this.printCategoryStripMenuItem.Name = "printCategoryStripMenuItem";
            this.printCategoryStripMenuItem.Click += new System.EventHandler(this.menuPrintCategory_Click);
            // 
            // ctxMenuTreeNote
            // 
            this.ctxMenuTreeNote.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.moveToDesktopStripMenuItem,
            this.copyToDesktopStripMenuItem,
            this.toolStripSeparator8,
            this.removeNoteStripMenuItem,
            this.toolStripSeparator9,
            this.printToolStripMenuItem});
            this.ctxMenuTreeNote.Name = "ctxMenuTreeNote";
            resources.ApplyResources(this.ctxMenuTreeNote, "ctxMenuTreeNote");
            // 
            // moveToDesktopStripMenuItem
            // 
            this.moveToDesktopStripMenuItem.Name = "moveToDesktopStripMenuItem";
            resources.ApplyResources(this.moveToDesktopStripMenuItem, "moveToDesktopStripMenuItem");
            this.moveToDesktopStripMenuItem.Click += new System.EventHandler(this.MenuMoveToDesktop);
            // 
            // copyToDesktopStripMenuItem
            // 
            this.copyToDesktopStripMenuItem.Name = "copyToDesktopStripMenuItem";
            resources.ApplyResources(this.copyToDesktopStripMenuItem, "copyToDesktopStripMenuItem");
            this.copyToDesktopStripMenuItem.Click += new System.EventHandler(this.menuCopyToDesktop_Click);
            // 
            // toolStripSeparator8
            // 
            this.toolStripSeparator8.Name = "toolStripSeparator8";
            resources.ApplyResources(this.toolStripSeparator8, "toolStripSeparator8");
            // 
            // removeNoteStripMenuItem
            // 
            resources.ApplyResources(this.removeNoteStripMenuItem, "removeNoteStripMenuItem");
            this.removeNoteStripMenuItem.Name = "removeNoteStripMenuItem";
            this.removeNoteStripMenuItem.Click += new System.EventHandler(this.menuRemoveNote_Click);
            // 
            // toolStripSeparator9
            // 
            this.toolStripSeparator9.Name = "toolStripSeparator9";
            resources.ApplyResources(this.toolStripSeparator9, "toolStripSeparator9");
            // 
            // printToolStripMenuItem
            // 
            resources.ApplyResources(this.printToolStripMenuItem, "printToolStripMenuItem");
            this.printToolStripMenuItem.Name = "printToolStripMenuItem";
            this.printToolStripMenuItem.Click += new System.EventHandler(this.printStripMenuItem_Click);
            // 
            // fontDialog
            // 
            this.fontDialog.FontMustExist = true;
            this.fontDialog.ShowColor = true;
            // 
            // ArchiveForm
            // 
            resources.ApplyResources(this, "$this");
            this.Controls.Add(this.toolStripContainer1);
            this.Controls.Add(this.splitter1);
            this.Controls.Add(this.treeArchive);
            this.Name = "ArchiveForm";
            this.Load += new System.EventHandler(this.ArchiveForm_Load);
            this.Closing += new System.ComponentModel.CancelEventHandler(this.ArchiveForm_Closing);
            this.toolStripContainer1.BottomToolStripPanel.ResumeLayout(false);
            this.toolStripContainer1.BottomToolStripPanel.PerformLayout();
            this.toolStripContainer1.ContentPanel.ResumeLayout(false);
            this.toolStripContainer1.TopToolStripPanel.ResumeLayout(false);
            this.toolStripContainer1.TopToolStripPanel.PerformLayout();
            this.toolStripContainer1.ResumeLayout(false);
            this.toolStripContainer1.PerformLayout();
            this.statusStrip1.ResumeLayout(false);
            this.statusStrip1.PerformLayout();
            this.panel1.ResumeLayout(false);
            this.ctxMenuArchNote.ResumeLayout(false);
            this.toolStripRTF.ResumeLayout(false);
            this.toolStripRTF.PerformLayout();
            this.ctxMenuCategory.ResumeLayout(false);
            this.ctxMenuTreeNote.ResumeLayout(false);
            this.ResumeLayout(false);

		}
		#endregion

        /// <summary>
        /// Text assigned to note displayed on DeskNote RichTextBox
        /// Set - plain text or RTF text
        /// Get - plain text only
        /// </summary>
        internal string TextEx
        {
            set
            {
                if (value.StartsWith(RtfConverter.RTF_TAG))
                    txtNote.Rtf = value;
                else
                    txtNote.Text = value;
            }
        }

		private void treeArchive_AfterCollapse(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			CategoryNode cn = (CategoryNode) e.Node;
			cn.ImageIndex = COLLAPSED_IMAGE;
			cn.SelectedImageIndex = COLLAPSED_IMAGE;
			deskNotes.UpdateCategoryExpanded(cn.ID, cn.IsExpanded);
		}

		private void treeArchive_AfterExpand(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			CategoryNode cn = (CategoryNode) e.Node;
			cn.ImageIndex = EXPANDED_IMAGE;
			cn.SelectedImageIndex = EXPANDED_IMAGE;
			deskNotes.UpdateCategoryExpanded(cn.ID, cn.IsExpanded);
		}

		private void ArchiveForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			this.UpdateChangedNote();
			RememberSelectedNode(treeArchive.SelectedNode);
            Rectangle r = new Rectangle(this.Left, this.Top, this.Width, this.Height);
            Properties.Settings.Default.ArchivePosition = r;
            Properties.Settings.Default.ArchiveSplitter = splitter1.Location.X;
			deskNotes.SaveArchive();
		}

		/// <summary>
		/// After node is selected in the tree appropriate text must be displayed in Note's rich text box.
		/// For this time rich text box TextChanged event handler is disabled.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void treeArchive_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			txtNote.TextChanged -= new System.EventHandler(this.Note_TextChanged);
			currentNoteChanged = false;
			if (e.Node is ArchivedNoteNode)
			{
                Properties.Settings settings = Properties.Settings.Default;

				ArchivedNoteNode archNote = (ArchivedNoteNode) e.Node;
				//this.txtNote.Rtf = archNote.NoteText;
				this.lblArchivedTime.Text = archNote.ArchivedTimeText;
                this.lblModificationTime.Text = archNote.LastModificationTimeText;
                if (settings.OriginalLook)
                {
                    txtNote.ForeColor = archNote.PanelTextColor;
                    txtNote.BackColor = archNote.PanelBackColor;
                    txtNote.Font = null != archNote.Font ? archNote.Font : settings.Font;
                }
                else
                {
                    txtNote.ForeColor = settings.TextColor;
                    txtNote.BackColor = settings.BackgroundColor;
                    txtNote.Font = settings.Font;
                }
                this.TextEx = archNote.NoteText;
			} 
			else 
			{
				this.txtNote.BackColor = this.backColor;
				this.txtNote.Text = "";
				this.lblArchivedTime.Text = "";
			}
			DeskNotesTreeNode node = (DeskNotesTreeNode) e.Node;
			//Show or hide edited picture according to node's state
			if (node.Edited)
				btnSave.Enabled = true;
			else
                btnSave.Enabled = false;

			txtNote.TextChanged += new System.EventHandler(this.Note_TextChanged);
		}

		private void btnClose_Click(object sender, System.EventArgs e)
		{
			this.Hide();
		}

		/// <summary>
		/// Displays context menu based on underlying node.
		/// Category and Note have own context menus.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void treeArchive_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			base.OnMouseUp(e);
			
			if (e.Button == MouseButtons.Right)
			{
				TreeNode node = treeArchive.GetNodeAt(e.X, e.Y);
				if (null == node) //When user clicks outside of node then do nothing.
					return;

				treeArchive.SelectedNode = node;
				
				if (node is ArchivedNoteNode)
				{
					ctxMenuTreeNote.Show(treeArchive, new Point(e.X, e.Y));
				} 
				else 
				{
					if (null == node.Parent)
						//disable Remove menu item for root node
						removeStripMenuItem.Enabled = false;
					else
                        removeStripMenuItem.Enabled = true;
					ctxMenuCategory.Show(treeArchive, new Point(e.X, e.Y));
				}
			}
		}

		/// <summary>
		/// Archive is loaded from DataSet into tree structure and assigned 
		/// into treeview treeArchive
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ArchiveForm_Load(object sender, System.EventArgs e)
		{

			TreeNode sNode = new TreeNode();
			treeArchive.Nodes.Add(deskNotes.CreateTreeFromArchive(ref sNode));
			txtNote.Font = Properties.Settings.Default.Font;
			textColor = txtNote.ForeColor;
			backColor = txtNote.BackColor;
			treeArchive.SelectedNode = sNode;
		}

		/// <summary>
		/// Only Category label can be edited
		/// Notes label cannot and action is cancelled at this case.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void treeArchive_BeforeLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e)
		{
			if(e.Node is ArchivedNoteNode)
			{
				e.CancelEdit = true;
			}
		}

		/// <summary>
		/// When Category label was edited then Archive dataset is updated with changes
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void treeArchive_AfterLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e)
		{
			if (e.Node is CategoryNode)
			{
				CategoryNode cat = (CategoryNode) e.Node;
				deskNotes.UpdateCategory(cat.ID, cat.Parent, e.Label, cat.IsExpanded);
				NeedSave();
				cat.Edited = true;
			}
		}

		/// <summary>
		/// When Note's text is changed for a very first time ie first key is pressed 
		/// then status of Note's node is changed and Edited flag is set.
		/// This event handler is called for every char pressed!
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void Note_TextChanged(object sender, System.EventArgs e)
		{
			if (currentNoteChanged == false)
			{
				currentNoteChanged = true;
				NeedSave();
				ArchivedNoteNode archNote = (ArchivedNoteNode)treeArchive.SelectedNode;
				archNote.Edited = true;
			}
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			try
            {
                UpdateChangedNote();
                deskNotes.SaveArchive();
			} 
			catch (System.Security.SecurityException se)
			{
				string text = string.Format(MainResource.GetString("ERROR_WRITE_DATA"), Properties.Settings.Default.ArchiveFile, se.Message + "\n");
				if ( DialogResult.No == MessageBox.Show(this, text, MainResource.GetString("APP_TITLE"),
				MessageBoxButtons.YesNo, MessageBoxIcon.Error))
				return;
			}
			this.ArchiveSaved();
		}

		/// <summary>
		/// When Note's text area is being left then changed text is written
		/// into Archive dataset.
		/// !!! There can be a state when another app's window is selected
		/// then Leave event does not happen. Or form is made hidden,...
		/// These states must be handled elsewhere !!!!
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void txtNote_Leave(object sender, System.EventArgs e)
		{
			UpdateChangedNote();
		}

		private void ArchiveForm_Deactivate(object sender, System.EventArgs e)
		{
			//Console.WriteLine("Archive deactivated");
			UpdateChangedNote();
		}

        private void treeArchive_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            UpdateChangedNote();
        }
		/// <summary>
		/// Updates changed note when text or form lost its focus.
		/// </summary>
		private void UpdateChangedNote()
		{
			if (treeArchive.SelectedNode is ArchivedNoteNode)
			{
				ArchivedNoteNode node = (ArchivedNoteNode) treeArchive.SelectedNode;
				//if Note's node was CURRENTLY edited then update Arch. dataset.
				if ((node.Edited) && (currentNoteChanged))
				{
                    node.LastModificationTime = DateTime.Now;
					node.NoteText = txtNote.Rtf;
					deskNotes.UpdateArchivedNote(node, ((CategoryNode)node.Parent).ID);
					currentNoteChanged = false;
				}
			}
		}

		/// <summary>
		/// Saves ID of selected node into registry.
		/// </summary>
		/// <param name="selectedNode">Node selected in archive tree.</param>
		private void RememberSelectedNode(TreeNode selectedNode)
		{
			DeskNotesTreeNode node = (DeskNotesTreeNode) selectedNode;
			if (null != node)
			{
				Properties.Settings.Default.SelectedNodeId = node.ID;
				Properties.Settings.Default.SelectedNodeType = node.Type;
			}
		}

	
		/// <summary>
		/// Creates archived node for given note under given category
		/// </summary>
		/// <param name="category">Category under which note is archived</param>
		/// <param name="note">DeskNotes Note to be archived.</param>
		/// <returns></returns>
		private ArchivedNoteNode DoArchiving(CategoryNode category, Note note)
		{
			ArchivedNoteNode archNode = new ArchivedNoteNode(note); 
			category.Nodes.Add(archNode);
			deskNotes.ArchiveDeskNote(archNode, note);
			// Remove desknote from desktop and from dataset
			note.RemoveRecord();
			note.MyForm.Close();
			//treeArchive.SelectedNode = archNode;
			return archNode;
		}

		/// <summary>
		/// Archive into selected or default tree category.
		/// </summary>
		/// <param name="note">DeskNotes note to be archived directly</param>
		/// <returns></returns>
		internal bool Archive(Note note)
		{
			CategoryNode category;
			TreeNode node = treeArchive.SelectedNode;
			if (null == node) //set root node
				node = (TreeNode)treeArchive.Nodes.GetEnumerator().Current;
			
			if (node is ArchivedNoteNode)
				category = (CategoryNode) node.Parent;
			else
				category = (CategoryNode) node;

			ArchivedNoteNode archNode = DoArchiving(category, note);
			if (null != archNode)
			{
				archNode.Edited = true;
				NeedSave();
                treeArchive.SelectedNode = archNode;
				return true;
			}
			else
				return false;
		}

		/// <summary>
		/// Handles actual drop of data into tree view. Used by drag&drop during archiving and
		/// move of archived nodes too.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void treeArchive_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
		{
			ArchivedNoteNode archNode = null;
			CategoryNode category;

			TreeNode node = treeArchive.GetNodeAt(treeArchive.PointToClient(new Point(e.X, e.Y)));
			
			//if there is no node under cursor then use selected one or root node.
			if (null == node)
			{
				node = (TreeNode)treeArchive.SelectedNode;
				if (null == node) //set root node
					node = (TreeNode)treeArchive.Nodes.GetEnumerator().Current;
			}

			if (node is ArchivedNoteNode)
				category = (CategoryNode) node.Parent;
			else
				category = (CategoryNode) node;

			if (e.Data.GetDataPresent((new Note()).GetType()))
			{ //Archivation of desknote from desktop
				Note note = (Note)e.Data.GetData((new Note()).GetType());
				archNode = DoArchiving(category, note);
			} 
			else if (e.Data.GetDataPresent( (new CategoryNode()).GetType()))
			{ // Move whole category to another one
				CategoryNode movedCat = (CategoryNode) e.Data.GetData((new CategoryNode()).GetType());
				deskNotes.UpdateCategory(movedCat.ID, category, movedCat.Text, movedCat.IsExpanded);
				movedCat.Parent.Nodes.Remove(movedCat);
				category.Nodes.Add(movedCat);
				movedCat.Edited = true;
				NeedSave();
			} 
			else if (e.Data.GetDataPresent( (new ArchivedNoteNode()).GetType()))
			{ // Move one archived note under different category
				archNode = (ArchivedNoteNode) e.Data.GetData((new ArchivedNoteNode()).GetType());
				//CategoryNode parent = (CategoryNode) archNode.Parent;
				deskNotes.UpdateArchivedNote(archNode, category.ID);
				archNode.Parent.Nodes.Remove(archNode);
				category.Nodes.Add(archNode);
			}
			//if drop was really done then assign node and mark it edited and required to be saved.
			if (archNode != null)
			{
				archNode.Edited = true;
				NeedSave();
				treeArchive.SelectedNode = archNode;
				txtNote.DetectUrls = false;
				txtNote.DetectUrls = true;
			}
		    
		}

		private void treeArchive_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
		{
			if (e.Data.GetDataPresent(DataFormats.Text))
				e.Effect = DragDropEffects.Copy;
			else if ( (e.Data.GetDataPresent((new Note()).GetType())) ||
					(e.Data.GetDataPresent((new ArchivedNoteNode()).GetType())) ||
					(e.Data.GetDataPresent((new CategoryNode()).GetType())) )
				e.Effect = DragDropEffects.Move;
			else 
			{
				e.Effect = DragDropEffects.None;
			}
		}

		/// <summary>
		/// Node is expanded if data being dragged are correct and node under mouse is category.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void treeArchive_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
		{
			if ( (e.Data.GetDataPresent(DataFormats.Text)) ||
				(e.Data.GetDataPresent((new Note()).GetType())) ) 
			{
				DeskNotesTreeNode target = (DeskNotesTreeNode) treeArchive.GetNodeAt(treeArchive.PointToClient(new Point(e.X, e.Y)));
				if (target is CategoryNode)
				{
					target.Expand();
				}
			}
		}

		/// <summary>
		/// Remembers selected node. The node is then used when drag&drop inside tree starts.
		/// ItemDrag method does not change selection of node if node is not clicked before.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void treeArchive_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			TreeNode node = treeArchive.GetNodeAt(e.X, e.Y);
			if (null != node) //only when node was under the mouse when clicked
				selectedNode = node;
		}

		/// <summary>
		/// Moves nodes between categories in the archive form tree view. 
		/// Does not move root node
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void treeArchive_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
		{
			treeArchive.SelectedNode = selectedNode;			
			if (selectedNode is ArchivedNoteNode)
				this.DoDragDrop((ArchivedNoteNode)selectedNode, DragDropEffects.Move);
			else if (((CategoryNode)selectedNode).Parent != null)
				this.DoDragDrop((CategoryNode)selectedNode, DragDropEffects.Move);
		}

		/// <summary>
		/// Moves selected Note back to desktop, removes it from archive data 
		/// and saves the archive.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void MenuMoveToDesktop(object sender, System.EventArgs e)
		{
			ArchivedNoteNode node = (ArchivedNoteNode)treeArchive.SelectedNode;
			Note tempNote = deskNotes.GetNoteFromArchive(node.ID);
			if (deskNotes.ShowArchivedNote(tempNote))
			{
				if (deskNotes.RemoveArchivedNote(node.ID))
				{
					DeskNotesTreeNode parent = (DeskNotesTreeNode)node.Parent;
					parent.Edited = true;
					treeArchive.Nodes.Remove(node);
					this.NeedSave();
					treeArchive.SelectedNode = parent;
				}
			}
		}

		/// <summary>
		/// Removes archived note from archive
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void menuRemoveNote_Click(object sender, System.EventArgs e)
		{
			ArchivedNoteNode node = (ArchivedNoteNode)treeArchive.SelectedNode;
			if (deskNotes.RemoveArchivedNote(node.ID))
			{
				DeskNotesTreeNode parent = (DeskNotesTreeNode) node.Parent;
				parent.Edited = true;
				this.NeedSave();
				treeArchive.Nodes.Remove(node);
				treeArchive.SelectedNode = parent;
			}
		}
		/// <summary>
		/// Adds new category with default text under currently selected category.
		/// Marks new category as changed and makes it editable.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void menuNewCategory_Click(object sender, System.EventArgs e)
		{
			CategoryNode parent = (CategoryNode) treeArchive.SelectedNode;
			CategoryNode node = new CategoryNode(MainResource.GetString("DEFAULT_ARCHIVE_CATEGORY"));
			parent.Nodes.Add(node);
			deskNotes.AddArchiveCategory(node);
			this.NeedSave();
			node.Edited = true;
			treeArchive.SelectedNode = node;
			node.BeginEdit();
		}


		/// <summary>
		/// Removes selected category from tree and archive with all its
		/// subcategories and nodes.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void menuRemoveCategory_Click(object sender, System.EventArgs e)
		{
			CategoryNode node = (CategoryNode) treeArchive.SelectedNode;
			if (DialogResult.Yes == MessageBox.Show(this, MainResource.GetString("CONFIRM_CATEGORY_REMOVE"),
				MainResource.GetString("APP_TITLE"),MessageBoxButtons.YesNo,MessageBoxIcon.Question))
			{
				if (deskNotes.RemoveCategory(node.ID))
				{
					CategoryNode parent = (CategoryNode)node.Parent;
					parent.Edited = true;
					treeArchive.SelectedNode = parent;
					node.Remove();
					this.NeedSave();
				}
			}
		}

        // TODO - Copy to Desktop fails with exception for Archived time for Note archived in older version.

		/// <summary>
		/// Makes a copy of desknote on desktop.
		/// Archived one remains in the archive.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void menuCopyToDesktop_Click(object sender, System.EventArgs e)
		{
			ArchivedNoteNode node = (ArchivedNoteNode)treeArchive.SelectedNode;
			Note tempNote = deskNotes.GetNoteFromArchive(node.ID);
			deskNotes.ShowArchivedNote(tempNote);
		}

		private void insertTimeStripMenuItem_Click(object sender, System.EventArgs e)
		{
            System.Text.StringBuilder timeStamp = new System.Text.StringBuilder(deskNotes.TimeStamp);
            timeStamp.Insert(0, "\n");
            timeStamp.Append("\n");
            txtNote.SelectedText = timeStamp.ToString();
		}

		private void txtNote_LinkClicked(object sender, System.Windows.Forms.LinkClickedEventArgs e)
		{
			try
			{
				System.Diagnostics.Process.Start(e.LinkText);
			}
			catch (Exception ex)
			{
				MessageBox.Show(this, MainResource.GetString("CANNOT_OPEN_LINK") + ex.Message, MainResource.GetString("APP_TITLE"),
					MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
			}
		}

		private void printStripMenuItem_Click(object sender, System.EventArgs e)
		{
			try 
			{
				ArchivedNoteNode node = (ArchivedNoteNode) treeArchive.SelectedNode;
				deskNotes.PrintOneNote(node, null);
			} 
			catch (System.Drawing.Printing.InvalidPrinterException ex)
			{
				MessageBox.Show(this, string.Format(MainResource.GetString("PRINTER_NOT_AVAILABLE"),ex.Message), 
					MainResource.GetString("APP_TITLE"),
					MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void menuPrintPreview_Click(object sender, System.EventArgs e)
		{
			try 
			{
				ArrayList list = new ArrayList(treeArchive.SelectedNode.Nodes.Count);
				GetSubNodes(treeArchive.SelectedNode, ref list);
				ArchivedNoteNode[] notes = new ArchivedNoteNode[list.Count];
				list.CopyTo(notes);
				PrintPreviewForm printPreview = new PrintPreviewForm(deskNotes, notes);
				printPreview.Show();
			} 
			catch (System.Drawing.Printing.InvalidPrinterException ex)
			{
				MessageBox.Show(this, string.Format(MainResource.GetString("PRINTER_NOT_AVAILABLE"), ex.Message), 
					MainResource.GetString("APP_TITLE"),
					MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// Recursively fills children array with ArchivedNoteNodes
		/// </summary>
		/// <param name="node">Node to start from, should be category</param>
		/// <param name="children">List of ArchivedNoteNodes under given category and all its subcategories.</param>
		internal void GetSubNodes(TreeNode node, ref ArrayList children)
		{
			foreach (TreeNode child in node.Nodes)
			{
				if (child is ArchivedNoteNode)
					children.Add(child);
				else
					GetSubNodes( child, ref children );
			}
		}

		/// <summary>
		/// Prints all notes under given category
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void menuPrintCategory_Click(object sender, System.EventArgs e)
		{
			try 
			{
				ArrayList list = new ArrayList(treeArchive.SelectedNode.Nodes.Count);
				GetSubNodes(treeArchive.SelectedNode, ref list);
				ArchivedNoteNode[] notes = new ArchivedNoteNode[list.Count];
				list.CopyTo(notes);
                deskNotes.PrintNotes(notes, null); //(MenuItem) sender, this.menuPrint, this.menuPrint2);
			} 
			catch (System.Drawing.Printing.InvalidPrinterException ex)
			{
				MessageBox.Show(this, string.Format(MainResource.GetString("PRINTER_NOT_AVAILABLE"), ex.Message), 
					MainResource.GetString("APP_TITLE"),
					MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		
		}

        /// <summary>
        /// Change Bullet style for current selection
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBullet_Click(object sender, EventArgs e)
        {
            txtNote.SelectionBullet = !txtNote.SelectionBullet;
            Note_TextChanged(null, null);

        }

        private void txtNote_SelectionChanged(object sender, EventArgs e)
        {
            btnBullet.Checked = txtNote.SelectionBullet;
            if (null != txtNote.SelectionFont)
            {
                btnBold.Checked = txtNote.SelectionFont.Bold;
                btnItalic.Checked = txtNote.SelectionFont.Italic;
                btnStrike.Checked = txtNote.SelectionFont.Strikeout;
                btnUnderline.Checked = txtNote.SelectionFont.Underline;
            }
        }

        private void btnBold_Click(object sender, EventArgs e)
        {
            Font f = txtNote.SelectionFont;
            Font newFont;
            if (f.Bold)
                newFont = new Font(f, f.Style ^ FontStyle.Bold);
            else
                newFont = new Font(f, f.Style | FontStyle.Bold);
            txtNote.SelectionFont = newFont;
            btnBold.Checked = newFont.Bold;
            Note_TextChanged(null, null);
        }

        private void btnItalic_Click(object sender, EventArgs e)
        {
            Font f = txtNote.SelectionFont;
            Font newFont;
            if (f.Italic)
                newFont = new Font(f, f.Style ^ FontStyle.Italic);
            else
                newFont = new Font(f, f.Style | FontStyle.Italic);
            txtNote.SelectionFont = newFont;
            btnItalic.Checked = newFont.Italic;
            Note_TextChanged(null, null);
        }

        private void btnStrike_Click(object sender, EventArgs e)
        {
            Font f = txtNote.SelectionFont;
            Font newFont;
            if (f.Strikeout)
                newFont = new Font(f, f.Style ^ FontStyle.Strikeout);
            else
                newFont = new Font(f, f.Style | FontStyle.Strikeout);
            txtNote.SelectionFont = newFont;
            Note_TextChanged(null, null);
        }

        private void btnUnderline_Click(object sender, EventArgs e)
        {
            Font f = txtNote.SelectionFont;
            Font newFont;
            if (f.Underline)
                newFont = new Font(f, f.Style ^ FontStyle.Underline);
            else
                newFont = new Font(f, f.Style | FontStyle.Underline);
            txtNote.SelectionFont = newFont;
            btnUnderline.Checked = newFont.Underline;
            Note_TextChanged(null, null);
        }

        private void btnIndentRight_Click(object sender, EventArgs e)
        {
            txtNote.SelectionIndent += 5;
            Note_TextChanged(null, null);
        }

        private void btnIndentLeft_Click(object sender, EventArgs e)
        {
            if (txtNote.SelectionIndent >= 5)
            {
                txtNote.SelectionIndent -= 5;
                Note_TextChanged(null, null);
            }
        }

        /// <summary>
        /// Opens font dialog for current selection font.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFont_Click(object sender, EventArgs e)
        {
            fontDialog.Font = txtNote.SelectionFont;
            fontDialog.Color = txtNote.SelectionColor;
            if (DialogResult.OK == fontDialog.ShowDialog())
            {
                txtNote.SelectionFont = fontDialog.Font;
                txtNote.SelectionColor = fontDialog.Color;
                Note_TextChanged(null, null);
            }
        }

        private void btnBackColor_Click(object sender, EventArgs e)
        {
            colorDialog.Color = txtNote.BackColor;
            if (DialogResult.OK == colorDialog.ShowDialog())
            {
                txtNote.BackColor = colorDialog.Color;
                ArchivedNoteNode archNote = (ArchivedNoteNode)treeArchive.SelectedNode;
                archNote.PanelBackColor = colorDialog.Color;
                Note_TextChanged(null, null);
            }
        }

        private void toolbarStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.toolbarStripMenuItem.Checked)
                this.toolStripRTF.Show(); 
            else
                this.toolStripRTF.Hide();
        }

        private void statusbarStripMenuItem_Click(object sender, EventArgs e)
        {
            if (statusbarStripMenuItem.Checked)
                this.statusStrip1.Show();
            else
                this.statusStrip1.Hide();
        }

        private void ctxMenuArchNote_Opening(object sender, CancelEventArgs e)
        {
            statusbarStripMenuItem.Checked = statusStrip1.Visible;
            toolbarStripMenuItem.Checked = toolStripRTF.Visible;
        }

        private void txtNote_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control)
            {
                switch (e.KeyCode)
                {
                    case Keys.B:
                        if (e.Shift)
                        { // Ctrl+Shift+B makes line bullet
                            this.btnBullet_Click(sender, null);
                        }
                        else
                        {
                            this.btnBold_Click(sender, null);
                        }
                        break;
                    case Keys.I:
                        this.btnItalic_Click(sender, null);
                        // SuppressKeyPress has to be called because some unknown maybe 
                        // native control code interprets this as insert "\t" into text box.
                        e.SuppressKeyPress = true;
                        break;
                    case Keys.U:
                        this.btnUnderline_Click(sender, null);
                        break;
                    case Keys.S:
                        this.btnSave_Click(sender, null);
                        break;
                    case Keys.Oemcomma:
                        this.btnIndentLeft_Click(sender, null);
                        break;
                    case Keys.OemPeriod:
                        this.btnIndentRight_Click(sender, null);
                        break;
                }
            }
        }

        

      

       

		
	}
}
